package com.swgk.core.common.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.gyf.barlibrary.ImmersionBar;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.helper.AppManagerHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.SupportActivity;

/**
 * @author: 杨畔
 * @date: 2021/2/20 15:23
 * @Description: activity基类
 */
public abstract class BaseActivity<VM extends BaseViewModel, VDB extends ViewDataBinding>
        extends SupportActivity implements View.OnClickListener {

    protected VM mViewModel = null;
    protected VDB mBinding;
    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private static long lastClickTime;
    /**
     * activity堆栈管理
     */
    protected AppManagerHelper appManager = AppManagerHelper.getAppManagerHelper();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加activity 到栈
        appManager.addActivity(this);
        //设置状态栏
        initStatusBar();
        //绑定生命周期
        mBinding = DataBindingUtil.setContentView(this, layoutId());
        mBinding.setLifecycleOwner(this);
        //创建我们的ViewModel。
        createViewModel();
        initBaseObserve();
        //绑定EventBus
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }
        //页面逻辑
        init();
    }

    /**
     * 初始化状态栏
     */
    private void initStatusBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(statusBarDark())
                .fitsSystemWindows(false)
                .keyboardEnable(false)
                .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
                .init();
    }

    /**
     * 是否是暗色状态栏
     */
    protected boolean statusBarDark() {
        return false;
    }

    /**
     * 获取布局文件
     */
    protected abstract int layoutId();

    /**
     * 创建viewModel
     */
    protected abstract void createViewModel();

    /**
     * 初始化逻辑
     */
    protected abstract void init();

    /**
     * 是否使用eventBus
     */
    public boolean isUseEventBus() {
        return false;
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            onMultiClick(v);
        }
    }

    /**
     * 防重点击
     */
    protected void onMultiClick(View v){

    }

    /**
     * 重写getResources()方法，让APP的字体不受系统设置字体大小影响
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑eventBus
        if (isUseEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    //关闭软件盘
    public void closeKeybord() {
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 清除页面焦点
     */
    protected void clearAllFocus() {
        if (getCurrentFocus() != null) {
            getCurrentFocus().clearFocus();
        }
    }

    /**
     * 初始化默认Observer
     */
    private void initBaseObserve(){
        if(mViewModel==null){
            return;
        }
        mViewModel.mToast.observe(this,message -> MToast.showLongToast(this,message));
        mViewModel.mLoading.observe(this,type->{
            switch (type){
                case BaseViewModel.SHOW_LODING:
                    DialogHelper.showProgressDialog(this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case BaseViewModel.DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    break;
                case BaseViewModel.FINISH_AND_DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    finish();
                    break;
            }
        });
    }
}
